<template>
  <Container>
    <headForm :list="list" @onSearch="onSearch" slot="header"/>
    <Tabs v-model="tab">
      <Button size="large" class="item" type="primary" slot="extra" @click="setEditItem">政策设置</Button>
      <export-btn 
        :param="exportParam" 
        url='/salesman/identify_report/export'
        class="item" 
        slot="extra"></export-btn>
      <TabPane label="独立团队管理" name="tab1">
        <homeTable
          :list="list"
          ref="homeTable"
          @getData="getData"
          @onPageSizeChange="onPageSizeChange"
          @onPageChange="onPageChange"
          @onSelectionChange="onSelectionChange"
        />
        <Loader :status="list.status" @refresh="getData"></Loader>
      </TabPane>
    </Tabs>
    <setEditItem ref="setEditItem" @complete="getData"></setEditItem>
    <onEdit ref="onEdits" @complete="getData"></onEdit>
  </Container>
</template>
<script>
import homeTable from "~/homeTable"
import list from './list/IndependentTeamListData'
import setEditItem from './edit/setEditItem'
import onEdit from './edit/editItem'
import headForm from './list/headForm'

import { clearEmpty, formatMonth } from '@/util'
export default {
  name: 'setting/independentTeam/list',
  components: {
    setEditItem,
    onEdit,
    headForm,
    homeTable
  },
  data() {
    return {
      tab: 'tab1',
      list: list
    }
  },
  mounted() {
    this.$bus.$on('onEdit', this.onEdit)
    this.$bus.$on('onApprove', this.onApprove)
    this.getData()
  },
  destroyed() {
    this.$bus.$off()
  },
  methods: {
    onSearch() {
      this.list.currentPage = 1
      this.getData()
    },
    getData() {
      let param = {
        current: this.list.currentPage,
        size: this.list.pageSize,
        ...this.list.form,
        yearmonth: (formatMonth(this.list.form.yearmonth)).replace('-',''),
        isAuth: this.list.form.isAuth ? (this.list.form.isAuth == 1 ? true : false) : null
      }
      param = clearEmpty(param)
      this.list.status = 'loading'
      this.$ajaxGet('/salesman/identify_report/page', param)
        .then(data => {
          this.list.totalItem = Number(data.total)
          this.list.data = (data.records || []).map(item => {
            return {
              id: item.id,
              officeName: item.officeName,
              depName: item.depName,
              distributorName: item.distributorName,
              salesmanName: item.salesmanName,
              phoneNo: item.phoneNo,
              identifyDays: item.identifyDays,
              brandName: item.brandName,
              functionType: item.functionType,
              identifyStatus: item.identifyStatus,
              lastIdentifyDate: item.lastIdentifyDate,
              storeNum: item.storeNum,
              salesmanId: item.salesmanId,
              distributorId: item.distributorId,
              isAuth: item.isAuth,
              activeStoreCount: item.activeStoreCount,
              actualPanicBuyQuantity: item.actualPanicBuyQuantity,
              scanInQuantity: item.scanInQuantity
            }
          })
          this.list.status = 'pass'
        }).catch(() => {
          this.list.status = 'error'
        })
    },
    setEditItem() {
      this.$refs.setEditItem.openModal()
    },
    onEdit(row) {
      this.$refs.onEdits.updateForm(row)
    },
    onApprove(row) {
     let isAuths= row.isAuth ? false : true
       this.$Modal.confirm({
        title:'重置状态',
        content: '确定改变原本的状态吗？',
        onOk: () => {
          this.$ajaxPost(`/salesman/identify_report/setAuth?isAuth= ${isAuths}&salesmanId=${row.salesmanId}&distributorId=${row.distributorId}`).then((data) => {
            this.$Notice.success({
              title: '重置成功'
            })
            this.getData()
          })
        }
      })
    },
    onPageChange(number) {
      this.list.currentPage = number
      this.getData()
    },
    onPageSizeChange(pageSize) {
      this.list.currentPage = 1
      this.list.pageSize = pageSize
      this.getData()
    }
  },
  computed: {
    exportParam() {
      let param = {
        ...this.list.form,
        yearmonth: (formatMonth(this.list.form.yearmonth)).replace('-',''),
        isAuth: this.list.form.isAuth ? (this.list.form.isAuth == 1 ? true : false) : null
      }
      return param
    }
  }
}
</script>